import 'package:base_lib/common/bloc/base_bloc.dart';
import 'package:base_lib/index.dart';
import 'package:doctor_examine_lib/examine_list/examine_list_entity.dart'
    as ExamineBean;
import 'package:doctor_examine_lib/repository/https.dart';
import 'package:rxdart/rxdart.dart';

class ExamineListBloc extends BaseBloc {
  /// 医院id
  String _hospitalID;
  // 检查对象一级列表订阅
  BehaviorSubject<List<ExamineBean.Datum>> firstDataSourceSubject =
      BehaviorSubject();
  // 检查对象二级列表订阅
  BehaviorSubject<List<String>> secondDataSourceSubject = BehaviorSubject();
  // 检查对象三级列表订阅
  BehaviorSubject<List<ExamineBean.SubDatum>> thirdDataSourceSubject =
      BehaviorSubject();
  // 一级列表数组
  var _firstDataSource = List<ExamineBean.Datum>();
  // 二级列表数组
  var _sectionDataSource = List<String>();
  // 三级列表数组
  var _thirdDataSource = List<ExamineBean.SubDatum>();
  // 二三级列表字典
  var _dataSourceMap = Map<String, List<ExamineBean.SubDatum>>();
  // 二三级列表数组
  var _dataSourceList = List<ExamineBean.SubDatum>();
  // 被选择的二级
  var _selectedItem = "";
  @override
  void initState() async {
    super.initState();
  }

  @override
  void dispose() {
    firstDataSourceSubject.close();
    secondDataSourceSubject.close();
    thirdDataSourceSubject.close();
  }

  // 初始化数据
  void initailDatas(hospitalID) {
    _hospitalID = hospitalID;
    this.getHospitalClassList();
  }

  // 网路请求--获取检查项目列表
  void getHospitalClassList() async {
    var params = Map<String, dynamic>()..["id"] = _hospitalID;
    var result = await DoctorExamineHttp.getHospitalClassList(params);
    if (result.isSuccess) {
      _firstDataSource = result.data;
      firstDataSourceSubject.add(_firstDataSource);
    } else {
      firstDataSourceSubject.add([]);
    }
  }

  // 网路请求--获取某一检查项的子项目
  void fetchSecondItemsByClass(ExamineBean.Datum item) async {
    var params = Map<String, dynamic>()
      ..["examineClassID"] = item.examineClassId
      ..["hospitalID"] = item.hospitalId;

    var result = await DoctorExamineHttp.queryHospitalItemByClass(params);
    if (result.isSuccess) {
      _dataSourceList = result.data;
      Map<String, List<ExamineBean.SubDatum>> tempMap = {};
      _dataSourceList.forEach((element) {
        if (tempMap.keys.contains(element.attributeValue)) {
          var array = tempMap[element.attributeValue];
          array.add(element);
          tempMap[element.attributeValue] = array;
        } else {
          List<ExamineBean.SubDatum> array = List<ExamineBean.SubDatum>();
          if (_selectedItem == "") {
            _selectedItem = element.attributeValue;
          }
          array.add(element);
          tempMap[element.attributeValue] = array;
        }
      });
      _dataSourceMap = tempMap;
      _sectionDataSource = tempMap.keys.toList();
      _thirdDataSource = _dataSourceMap[_selectedItem];
      secondDataSourceSubject.add(_sectionDataSource);
      thirdDataSourceSubject.add(_thirdDataSource);
    }
  }

  // 判断是否是当前选择项
  bool isSelected(key) {
    return key == _selectedItem;
  }

  // 操作--选择二级目录
  void selectSecondItems(key) {
    _selectedItem = key;
    _thirdDataSource = _dataSourceMap[key];
    thirdDataSourceSubject.add(_thirdDataSource);
    secondDataSourceSubject.add(_sectionDataSource);
  }

  // 操作--选择三级目录
  void selecThirdItem(ExamineBean.SubDatum item, context) {
    Routers.pop(context, Parameters()..putObj("examineItem", item));
  }
}
